//
//  LinesTabBarItem.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/30.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "LinesTabBarItem.h"

@interface LinesTabBarItem ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLable;

@end

@implementation LinesTabBarItem

- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage {
    self = [self init];
    if (self) {
        
        _title = title;
        _image = image;
        _selectedImage = selectedImage;
        
        self.titleLable.text = title;
        self.imageView.image = image;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _font = Font(10);
        _titleColor = kContentColor;
        _selectedColor = kThemeColor;
    }
    return self;
}

- (void)setImage:(UIImage *)image {
    
    _image = image;
    self.imageView.image = image;
}

- (void)setTitle:(NSString *)title {
    
    _title = title;
    
    self.titleLable.text = title;
}

- (void)setSelected:(BOOL)selected {
    
    _selected = selected;
    if (selected) {
        self.imageView.image = self.selectedImage;
        
        self.titleLable.textColor = self.selectedColor;
    } else {
        self.imageView.image = self.image;
        self.titleLable.textColor = self.titleColor;
    }
}

- (void)setFont:(UIFont *)font {
    
    _font = font;
    self.titleLable.font = font;
}

- (void)setTitleColor:(UIColor *)titleColor {
    
    _titleColor = titleColor;
    
    self.titleLable.textColor = titleColor;
}

- (UIImageView *)imageView {
    
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        
        
        [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(self.mas_top).offset(5);
            make.width.mas_equalTo(24);
            make.height.mas_equalTo(24);
        }];
//        @weakify(_imageView);
//
//        @weakify(self);
//        [_imageView addObserverBlockForKeyPath:@"image" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
//            @strongify(_imageView);
//            @strongify(self);
//
//            UIImage *image = newVal;
//            if ([image isKindOfClass:[UIImage class]]) {
//
//                [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
//
//                    make.centerX.equalTo(self.mas_centerX);
//                    make.top.equalTo(self.mas_top).offset(5);
//                    make.width.mas_equalTo(image.size.width);
//                    make.height.mas_equalTo(image.size.height);
//                }];
//            }
//
//        }];
    }
    
    return _imageView;
}

- (UILabel *)titleLable {
    
    if (!_titleLable) {
        _titleLable = [UILabel new];
        _titleLable.adjustsFontSizeToFitWidth = YES;
        _titleLable.minimumScaleFactor = 0.8;
        _titleLable.numberOfLines = 2;
        _titleLable.font = self.font;
        _titleLable.textColor = self.titleColor;
        _titleLable.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLable];
        
        [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.imageView.mas_bottom);
            make.centerX.equalTo(self.mas_centerX);
            make.bottom.equalTo(self.mas_bottom);
            make.width.mas_equalTo(55);
        }];
    }
    return _titleLable;
}

@end
